package pers.wk.protocol;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 接口请求数据返回对象封装
 */
public class Response<T> {

    private final String status;
    private final String msg;
    private final Map<String, String> headers;
    private final T body;

    public Response(String status, String msg, Map<String, String> headers, T body) {
        if (StringUtils.isBlank(status)) {
            throw new IllegalArgumentException("Resp status must not be null!");
        } else {
            this.status = status;
            this.msg = msg;
            this.headers = headers;
            this.body = body;
        }
    }

    public Response(String status, String msg) {
        this(status, msg, (Map) null, null);
    }

    public Response(String status, T body) {
        this(status, (String) null, (Map) null, body);
    }

    public Response(String status, String msg, T body) {
        this(status, msg, (Map) null, body);
    }

    public static <T> Response<T> ok(T body) {
        return new Response(RespStatus.OK.value(), RespStatus.OK.getReasonPhrase(), (Map) null, body);
    }

    public static <T> Response<T> ok() {
        return new Response(RespStatus.OK.value(), RespStatus.OK.getReasonPhrase(), (Map) null, null);
    }

    public static <T> Response<T> fail(String status, String msg) {
        return new Response(status, msg, (Map) null, (Object) null);
    }

    public boolean isOk() {
        return RespStatus.OK.value().equalsIgnoreCase(this.status);
    }

    public String toJsonString() {
        return JSONObject.toJSONString(this);
    }

    public String getStatus() {
        return this.status;
    }

    public String getMsg() {
        return this.msg;
    }

    public Map<String, String> getHeaders() {
        return this.headers;
    }

    public T getBody() {
        return this.body;
    }

}
